package com.jstyle.jclifexd.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.jstyle.jclifexd.app.MyApplication;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/10/18.
 */

public class SportTrackView extends View {
    Paint mPaint;
    Paint roundPaint;
    public SportTrackView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        Log.i(TAG, "init: ");
        mPaint=new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(ScreenUtils.dip2px(context,6));
        mPaint.setColor(Color.GREEN);


        roundPaint=new Paint();
        roundPaint.setStyle(Paint.Style.FILL);
        roundPaint.setAntiAlias(true);
        roundPaint.setColor(Color.RED);//圆点颜色
    }

    List<Point>list;
    public void setPathData(List<Point>list){
        this.list=list;
        startAnim(list.size()-1);
    }

    private static final String TAG = "SportTrackView";
    Path path=new Path();
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(list==null||list.size()==0)return;

        Point point=list.get((int) drawPosition);
        int x=point.x;
        int y=point.y;
        if(drawPosition==0){
            path.moveTo(x,y);
        }else{
            path.lineTo(x,y);
        }
      //  canvas.drawPoint(x,y,mPaint);
       // if(drawPosition==list.size()-1)
        canvas.drawPath(path,mPaint);
        canvas.drawCircle(x, y,ScreenUtils.dip2px(MyApplication.instance(),5f),roundPaint);//绘制圆心光标
      //  path.reset();

    }
    public void startAnim(float end) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "position",
                0, end);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(5000);
        animatorSet.setInterpolator(new LinearInterpolator());
       animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
           @Override
           public void onAnimationUpdate(ValueAnimator animation) {
               float value= (float) animation.getAnimatedValue();
               if(value==list.size()-1)drawListener.onDrawFinished();

           }
       });
        animatorSet.setTarget(this);
        animatorSet.play(animator);
        animatorSet.start();
    }
    float drawPosition;
    public void setPosition(float position){
        this.drawPosition=position;
        postInvalidate();
    }
    DrawListener drawListener;
    public void setDrawListener(DrawListener drawListener){
        this.drawListener=drawListener;
    }
    public interface DrawListener{
        public void onDrawFinished();
    }
}
